<?php

namespace ali\easysdk;

use ali\easysdk\fund\trans\Client as TransClient;
use Alipay\EasySDK\Kernel\CertEnvironment;
use Alipay\EasySDK\Kernel\EasySDKKernel;

/**
 * 补充支付宝官方sdk
 */
class Factory
{
    public $config = null;
    public $kernel = null;
    public static $fund  = null;
    private static $instance;

    public function __construct($config) {
        if (!empty($config->alipayCertPath)) {
            $certEnvironment = new CertEnvironment();
            $certEnvironment->certEnvironment(
                $config->merchantCertPath,
                $config->alipayCertPath,
                $config->alipayRootCertPath
            );
            $config->merchantCertSN = $certEnvironment->getMerchantCertSN();
            $config->alipayRootCertSN = $certEnvironment->getRootCertSN();
            $config->alipayPublicKey = $certEnvironment->getCachedAlipayPublicKey();
        }

        $kernel = new EasySDKKernel($config);
        self::$fund = new Fund($kernel);
    }

    public static function setOptions($config)
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self($config);
        }
        return self::$instance;
    }

    private function __clone()
    {
    }

    /**
     * 资金能力
     * @return Fund|null
     */
    public static function fund() {
        return self::$fund;
    }
}

/**
 * 资金能力
 */
class Fund
{
    /**
     * @var EasySDKKernel
     */
    private $kernel;

    public function __construct($kernel)
    {
        $this->kernel = $kernel;
    }

    /**
     * 交易客户端
     * @return TransClient
     */
    public function trans()
    {
        return new TransClient($this->kernel);
    }
}